package com.android.qingsongpin.fragment;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.qingsongpin.utils.ObjectUtils;
import com.android.qingsongpin.R;
import com.android.qingsongpin.cache.DiskLruCacheHelper;
import com.android.qingsongpin.fragment.group.Fragment_Group;
import com.android.qingsongpin.fragment.group.Fragment_TabSearchList;
import com.android.qingsongpin.model.group.MainSearchSortData;
import com.android.qingsongpin.model.group.MainSearchSortDataCallback;
import com.android.qingsongpin.utils.ConstantsUrl;
import com.android.qingsongpin.utils.GsonUtils;
import com.android.qingsongpin.utils.HttpUtils;
import com.android.qingsongpin.utils.MyLog;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * App首页
 */

public class Fragment1 extends Fragment {


    @BindView(R.id.tab)
    TabLayout tabLayout;
    @BindView(R.id.pager)
    ViewPager viewPager;
    @BindView(R.id.main_top_title)
    TextView mainTopTitle;
    @BindView(R.id.main_top_bg)
    Toolbar mainTopBg;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.no_data)
    RelativeLayout noData;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    private View view;
    private FragmentActivity mcontext;
    private boolean networkConnected;
    private DiskLruCacheHelper helper;
    public Fragment1() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_fragment1, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        mainTopTitle.setText("轻松团");
        mainTopTitle.setTextColor(Color.parseColor("#ffffff"));
        mainTopBg.setBackgroundColor(Color.parseColor("#FF2741"));
        try {
            helper = new DiskLruCacheHelper(mcontext);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String cache_sort = helper.getAsString(ConstantsUrl.CACHE_HOME_SORTDATA_SEARCH);
        if (!TextUtils.isEmpty(cache_sort)) {
            MainSearchSortData response = GsonUtils.parseJSON(cache_sort, MainSearchSortData.class);
            mListSort.clear();
            List<MainSearchSortData.DataBean> data = response.getData();
            mListSort.add(new MainSearchSortData.DataBean("", "首页"));
            if (response.getData() != null && response.getData().size() > 0) {
                mListSort.addAll(data);
            }
            initUI();
            progress.setVisibility(View.GONE);
        } else {

            GetSortData();
        }
        return view;
    }

    private void initUI() {

        GroupFragmentPagerAdapter pagerAdapter = new GroupFragmentPagerAdapter(mcontext.getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
    }

    @OnClick(R.id.click_resetnetwork_refresh)
    public void onClick() {
        ObjectUtils.GetDataNet(clickResetnetwork, progress, 3);
        GetSortData();
    }
    private class GroupFragmentPagerAdapter extends FragmentPagerAdapter {


        public GroupFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                return Fragment_Group.newInstance();
            } else {
                return Fragment_TabSearchList.newInstance(position, mListSort.get(position).getId(), mListSort);
            }
        }

        @Override
        public int getCount() {
            return mListSort.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mListSort.get(position).getCatname();
        }
    }
    /**
     * 商品分类列表
     */
    private List<MainSearchSortData.DataBean> mListSort = new ArrayList<>();

    private void GetSortData() {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        String address = ConstantsUrl.domain + ConstantsUrl.SORTLISTDATA;
        MyLog.e("地址", address);
        if (networkConnected) {
            HttpUtils.get(address, null, new MainSearchSortDataCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(MainSearchSortData response, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
                    if (response.getCode() == 0) {
                        helper.put(ConstantsUrl.CACHE_HOME_SORTDATA_SEARCH, response.toString());
                        mListSort.clear();
                        List<MainSearchSortData.DataBean> data = response.getData();
                        mListSort.add(new MainSearchSortData.DataBean("", "首页"));
                        if (response.getData() != null && response.getData().size() > 0) {
                            mListSort.addAll(data);
                        }
                        initUI();
                    } else {
                        noData.setVisibility(View.VISIBLE);
                        noDataTv.setText(response.getMsg());
                        noDataTv.setVisibility(View.VISIBLE);
                    }
                }
            });
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
            ObjectUtils.toast(mcontext, "连接连接出现异常");
        }
    }
}



